package fanshe;


import java.lang.reflect.Field;
import java.lang.reflect.Method;

public class Demo02 {
    public static void main(String[] args) throws Exception {
        //最常见的方法用反射创建Person对象
        Class clazz = Class.forName("fanshe.Person");
        Object o = clazz.newInstance();

        Method show = clazz.getMethod("show");
        show.invoke(o);

        Field name = clazz.getDeclaredField("name");
        name.setAccessible(true);
        name.set(o, "张三");
        Field sex = clazz.getField("sex");
        sex.set(o, "男");
        System.out.println(name.get(o));
        System.out.println(sex.get(o));

        /**
         * 反射创建对象的另外两种方式
         */
        Class clazz1 = Person.class;
        Person p = new Person();
        Class clazz2 = p.getClass();
    }
}
